package com.sunk.springcloud.service.Impl;

import com.sunk.springcloud.dao.OrderDao;
import com.sunk.springcloud.domain.Order;
import com.sunk.springcloud.service.AccountService;
import com.sunk.springcloud.service.OrderService;
import com.sunk.springcloud.service.StroageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private StroageService stroageService;
    @Resource
    private AccountService accountService;

    /**
     * 创建订单->调用库存服务减扣库存->调用账户服务扣减账户余额->修改订单状态
     * @param order
     */
    @GlobalTransactional(name="fsp-create-order",rollbackFor = Exception.class)
    @Override
    public void create(Order order) {
        log.info("-------->开始新建订单");
        orderDao.create(order);
        log.info("-------->开始调用库存，做扣减");
        stroageService.decrease(order.getProductId(),order.getCount());
        log.info("-------->扣减完成");

        log.info("-------->开始扣减账户");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("-------->扣减账户完成");


        log.info("-------->修改订单状态开始");
        orderDao.update(order.getUserId(),0);
        log.info("-------->修改订单状态结束");

    }
}
